package com.wuqi.dev.exceptiondemo;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
 * Created by Risky57 on 2017/5/3.
 */

public class AllExceptionCatch implements Thread.UncaughtExceptionHandler {



    @Override
    public void uncaughtException (Thread t, Throwable e) {
        Log.d("TAGGG", "捕获到了全局的异常");
        Log.d("AllExceptionCatch", t.getName());
        File storageDirectory = Environment.getExternalStorageDirectory();
        File log = new File(storageDirectory, "exception.log");
        if (!log.exists()) {
            try {
                log.createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        try {
            FileOutputStream fos = new FileOutputStream(log);
            PrintStream ps = new PrintStream(fos);
            e.printStackTrace(ps);
            ps.flush();
            ps.close();




        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    }
}
